import { Component, OnInit, forwardRef, ViewChild, Inject, Output, EventEmitter } from '@angular/core';
import { SelectImageAddGridComponent } from './select-image-add-grid/select-image-add-grid.component';
import { path } from '../../../../../path.config';
import { masterService } from '../../../../../../lib/master.service';
import { saveVarService } from '../../../../../../lib/saveVar.service';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { AppComponent } from '../../../../../app.component';

@Component({
    selector: 'app-select-image-add2',
    templateUrl: './select-image-add.component.html',
    styleUrls: ['./select-image-add.component.css'],
    providers: [masterService]
})
export class SelectImageAdd2Component implements OnInit {
    @Output() SelectImage = new EventEmitter<any>();

    @ViewChild('selectImageGrid2') public selectImageGrid: SelectImageAddGridComponent;
    @ViewChild('selectImageGridModal') public selectImageGridModal: ModalDirective;
    // 获取列表项api
    private getList = path.materialManagement.imageManagement.getImgList;
    // 搜索传参的对象
    public filter: any = {
        imageName: '',
        pageSize: '10',
        pageIndex: '1'
    }

    constructor(private _masterService: masterService,
        public _saveVar: saveVarService,
        @Inject(forwardRef(() => AppComponent)) public app: AppComponent) { }

    ngOnInit() {
        this._masterService
            .getData(this.getList, this.filter)
            .subscribe((res) => {
                if (res.data) {
                    this.selectImageGrid.rows = res.data.items;
                    this.selectImageGrid.totalItems = res.data.pagination.totalCount;
                    this.selectImageGrid.page = parseInt(res.data.pagination.pageIndex);
                    this.selectImageGrid.itemsPerPage = res.data.pagination.pageSize;
                    this.selectImageGrid.cdRef.detectChanges();
                }
            })
    }

    // 过滤grid数据
    public selectListItem() {
        this._masterService
            .getData(this.getList, this.filter)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.selectImageGrid.rows = res.data.items;
                    this.selectImageGrid.totalItems = res.data.pagination.totalCount;
                    this.selectImageGrid.page = parseInt(res.data.pagination.pageIndex);
                    this.selectImageGrid.itemsPerPage = res.data.pagination.pageSize;
                    this.selectImageGrid.cdRef.detectChanges();
                    this.app.toastSuccess('筛选成功！');
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    public openAddModal() {
        this.selectImageGridModal.show();
    }

    public closeAddModal(obj) {
        this.selectImageGridModal.hide();
        this._saveVar.homeUpAddPicId = obj.id;
        this._saveVar.homeUpAddPicUrl = obj.url;
        this.SelectImage.emit(obj.url);
        this.app.toastSuccess('图片选择成功！');
    }
}
